package com.song.server;

import org.armedbear.lisp.Interpreter;
import org.armedbear.lisp.LispObject;

public class LSPManager {
	String path;
	Interpreter interpreter;
	long btime;
	public LSPManager(String pathname){
		tic();
		System.out.print("create lisp interpreter...");
		path = pathname;
		interpreter = Interpreter.createInstance();
		toc();
	}
	
	public boolean loadLSPPackage(){
		System.out.println("loading LSP package...");
		try{
			tic();
			System.out.print("loading abcl html.abcl ...");
			interpreter.eval("(load \""+path+"/html.abcl\")");
			System.out.print("loading abcl website-dir.abcl ...");
			interpreter.eval("(load \""+path+"/website-dir.abcl\")");
			interpreter.eval("(load-website-dir \""+path+"\")");
			toc();
			return true;
		}catch (Throwable t){            
			System.out.println("exception!");
			t.printStackTrace();
			return false;
		}
	}
	
	public boolean initWebsiteDir(){
		
		try{
			tic();
			System.out.print("init-website-dir...");
			interpreter.eval("(init-website-dir \""+path+"\")");
			toc();
			return true;
		}catch (Throwable t){            
			System.out.println("exception!");
			t.printStackTrace();
			return false;
		}
	}
	public boolean publish(String lispname,String... func){
		try{
			tic();
			System.out.print("compile-file lisp "+lispname+".lisp ...");
			interpreter.eval("(compile-file \""+path+"/"+lispname+".lisp\")");
			toc();
			String cmplispname = lispname+".abcl";
			tic();
			System.out.print("loading lisp "+cmplispname+" ...");
			interpreter.eval("(load \""+path+"/"+cmplispname+"\")");
			toc();
			tic();
			System.out.print("publish-lsp "+lispname+"...");
			interpreter.eval("(publish-lsp \""+lispname+"\")");
			toc();
			for(int i=0;i<func.length;i++){
				System.out.print("publish-page "+func[i]+"...");
				interpreter.eval("(publish-page '"+func[i]+")");
				toc();
			}
			tic();
			System.out.print("flush-website-dir...");
			interpreter.eval("(flush-website-dir \""+path+"\")");
			toc();
			return true;
		}catch (Throwable t){            
			System.out.println("exception!");
			t.printStackTrace();
			return false;
		}
	}
	
	public String runLSP(String requests[]){
		LispObject has = interpreter.eval("(has-page '"+requests[0]+")");
		if(!has.getBooleanValue())return "404";
		try{
			String repl = "("+requests[0];
			for(int i=1;i<requests.length;i+=2){
				repl += " :"+requests[i]+" \""+requests[i+1]+"\"";
			}
			repl += ")";
			tic();
			System.out.print("run repl:"+repl+" ...");
			LispObject lo = interpreter.eval(repl);
			toc();
			return lo.getStringValue();
		}catch (Throwable t){            
			System.out.println("exception!");
			t.printStackTrace();
			return "500";
		}
		
	}
	
	public void removePage(String page){
		tic();
		System.out.print("remove page function name "+page+"...");
		interpreter.eval("(remove-page '"+page+")");
		toc();
	}
	public void renamePage(String page,String to){
		tic();
		System.out.print("rename page function "+page+" to "+to+"...");
		interpreter.eval("(rename-page '"+page+" '"+to+")");
		toc();
	}
	public void removeLSP(String lsp){
		tic();
		System.out.print("remove LSP function name "+lsp+"...");
		interpreter.eval("(remove-lsp \""+lsp+"\")");
		toc();
	}
	public void renameLSP(String lsp,String to){
		tic();
		System.out.print("rename LSP  "+lsp+" to "+to+"...");
		interpreter.eval("(rename-lsp \""+lsp+"\" \""+to+"\")");
		toc();
	}
	
	public void tic(){
		btime = System.currentTimeMillis();
	}
	public void toc(){
		System.out.println(" cost "+(System.currentTimeMillis()-btime)+"ms");
	}
}
